//日期封装  
interface TimeFormatProps{
    date:Date;
    format?:string
}

const TimeFormat:React.FC<TimeFormatProps>=({
    date,
    format='YYYY-MM-DD HH:mm:ss'
})=>{
    //补零函数
    const padZero = (num:number):string=>{
        return num.toString().padStart(2,'0')
    }
   //提取日期各部分
   const year = date.getFullYear()
   const month = padZero(date.getMonth()+1)
   const day =padZero(date.getDate())
   const hours = padZero(date.getHours())
   const minutes = padZero(date.getMinutes())
   const seconds = padZero(date.getSeconds())   
   
   //替换格式字符串
   let formattedTime = format 
   formattedTime = formattedTime.replace(/YYYY/g,year.toString())
   formattedTime = formattedTime.replace(/MM/g,month)
   formattedTime = formattedTime.replace(/DD/g,day)
   formattedTime = formattedTime.replace(/HH/g,hours)
   formattedTime = formattedTime.replace(/mm/g,minutes)
   formattedTime = formattedTime.replace(/ss/g,seconds)

   return <span>{formattedTime}</span>
}

export default TimeFormat